/**
 * 客户资料维护  客户联系人 - 编辑页面
 * @author miaoyl
 * @date 2018/01/15
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            isDetail: false,
            form: {
                GWAKL004: null,
                GWALORIG_NAME: null,
                GWALORIU_NAME: null,
                MOBILE: null
            },
            disabled: true,
            rules: {
                GWAL001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                /*GWAL002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],*/
                GWAL003: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAL021: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAL020: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWALORIU_NAME:[
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWALORIG_NAME:[
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWALDATE:[
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                /*GWAL019: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAL010: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAL011: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAL012: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAL013: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAL014: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAL015: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAL018: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWALACTI: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ]*/
            },
            selectOptions: {
                gwal003Options: app.datas_('crm_gwai300_gwal003'),
                /*gwal010Options:[],
                 gwal011Options:[],*/
                gwal012Options: [],
                gwal013Options: [],
                gwal014Options: [],
                gwal015Options: [],
                gwal016Options: [],
                gwal017Options: [],
                sys_y_n: app.datas_("sys_y_n"),
            }
        },
        callObj,
        pageName,
        pageVue
    ;

    var Gwai300DetailEdit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai300_detail_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        editData: function () {
            app.ajax({
                xurl: 'crm/gwai300/editDetail',
                xdata: vueData.form,
                loading: true,
                okmsg: '客户联系人信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        callObj.obj.update(result.xdata);
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },

        addData: function () {
            app.ajax({
                xurl: 'crm/gwai300/addDetail',
                xdata: vueData.form,
                loading: true,
                okmsg: '客户联系人信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        if (pageName == 'gwai301') {
                            app.pageModule('gwai301').reloadTableCurr();
                        } else {
                            app.pageModule('gwai300').reloadDetailTableCurr();
                        }
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },

        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            callObj = obj;
            pageName = obj.pageName;
            if(obj.is_detail){
                vueData.isDetail = true;
            }else {
                vueData.isDetail = false;
            }
            if (obj.pageName == 'gwai301') {
                vueData.disabled = false;
                pageName = obj.pageName;
            } else {
                vueData.disabled = true;
            }
            if (obj.is_add) {
                vueData.isAdd = true;
                // app.log(obj);
                app.ajax({
                    xurl: 'crm/gwai300/getAddDetailData',
                    xdata: {},
                    cb: function (result) {
                        if (result.ok) {
                            app.extendUpLow({
                                target: vueData.form,
                                res: [result.data.data,{
                                    // GWAL001: obj.gwal001,
                                    // GWAL002: obj.gwal002,
                                    GWAL010: 'ASIA',
                                    GWAL011: 'CN',
                                    GWALACTI: 'Y'
                                }],
                                reset: true
                            });
                        }
                    }
                });
            } else {
                vueData.isAdd = false;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.obj.data,
                    reset: true
                });
                self.getAddress();
            }

        },

        getAddress:function () {
            vueData.selectOptions.gwal014Options = [];
            if(vueData.form.GWAL013 != '' && vueData.form.GWAL013 != null) {
                app.ajax({
                    xurl: 'crm/gwai300/getGwadList',
                    xdata: {gwad001: '5', gwad003: vueData.form.GWAL013, type: '4'},
                    cb: function (result) {
                        vueData.selectOptions.gwal014Options.pushArray(result.data.data);
                    }
                });
            }
            vueData.selectOptions.gwal015Options = [];
            if(vueData.form.GWAL014 != '' && vueData.form.GWAL014 != null) {
                app.ajax({
                    xurl: 'crm/gwai300/getGwadList',
                    xdata: {gwad001: '6', gwad003: vueData.form.GWAL014, type: '5'},
                    cb: function (result) {
                        vueData.selectOptions.gwal015Options.pushArray(result.data.data);
                    }
                });
            }
            vueData.selectOptions.gwal016Options = [];
            if(vueData.form.GWAL015 != '' && vueData.form.GWAL015 != null) {
                app.ajax({
                    xurl: 'crm/gwai300/getGwadList',
                    xdata: {gwad001: '7', gwad003: vueData.form.GWAL015, type: '6'},
                    cb: function (result) {
                        vueData.selectOptions.gwal016Options.pushArray(result.data.data);
                    }
                });
            }
            vueData.selectOptions.gwal017Options = [];
            if(vueData.form.GWAL016 != '' && vueData.form.GWAL016 != null) {
                app.ajax({
                    xurl: 'crm/gwai300/getGwadList',
                    xdata: {gwad001: '8', gwad003: vueData.form.GWAL016, type: '7'},
                    cb: function (result) {
                        vueData.selectOptions.gwal017Options.pushArray(result.data.data);
                    }
                });
            }
        },

        _ready: function () {
            vueData.form = app.fields_(['crm_gwai300_detail'], vueData.form);
            pageVue = new Vue({
                el: '#gwai300_detail_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        layer.close(callObj.layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                            }
                        });
                    },
                    openKh: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config:{
                                    method:'q_gwak001',
                                    columnList:[
                                        {label:'客户编号',prop:'GWAK001',width:150},
                                        {label:'客户简称',prop:'GWAKL003',width:150},
                                        {label:'客户全称',prop:'GWAKL004',width:150}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWAL001 = data.GWAK001;
                                    vueData.form.GWAKL004 = data.GWAKL004;
                                    app.ajax({
                                        xurl: 'crm/gwai300/getMaxGwal002',
                                        xdata: {GWAL001: vueData.form.GWAL001},
                                        cb: function (result) {
                                            var data = 1;
                                            if (result.data.data != '') {
                                                data = result.data.data;
                                            }
                                            vueData.form.GWAL002 = data;
                                        }
                                    });
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择客户',
                                area: ['600px', '430px']
                            }
                        });
                    },
                    openGwal023: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config:{
                                    method:'q_gwal021',
                                    columnList:[
                                        {label: '姓名', prop: 'GWAL021', width: '150'},
                                        {label: '电话', prop: 'GWAL020', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWAL023 = data.GWAK021;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择上级领导',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    clearGwal001:function(){
                        vueData.form.GWAL002 = "";
                        vueData.form.GWAL001 = "";
                    },
                    changeGwal013: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.gwal013Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.GWAL012 = obj.GWAD003;
                        vueData.form.NAME013 = obj.GWADL004;
                        pageVue.form.GWAL014 = "";
                        pageVue.form.GWAL015 = "";
                        pageVue.form.GWAL016 = "";
                        pageVue.form.GWAL017 = "";
                        pageVue.form.GWAL018 = "";
                        vueData.selectOptions.gwal014Options = [];
                        app.ajax({
                            xurl: 'crm/gwai300/getGwadList',
                            xdata: {gwad001: '5', gwad003: vueData.form.GWAL013, type: '4'},
                            cb: function (result) {
                                vueData.selectOptions.gwal014Options.pushArray(result.data.data);
                            }
                        });
                    },
                    changeGwal014: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.gwal014Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.NAME014 = obj.GWADL004;
                        pageVue.form.GWAL015 = "";
                        pageVue.form.GWAL016 = "";
                        pageVue.form.GWAL017 = "";
                        pageVue.form.GWAL018 = "";
                        vueData.selectOptions.gwal015Options = [];
                        app.ajax({
                            xurl: 'crm/gwai300/getGwadList',
                            xdata: {gwad001: '6', gwad003: vueData.form.GWAL014, type: '5'},
                            cb: function (result) {
                                vueData.selectOptions.gwal015Options.pushArray(result.data.data);
                            }
                        });
                    },
                    changeGwal015: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.gwal015Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.NAME015 = obj.GWADL004;
                        pageVue.form.GWAL016 = "";
                        pageVue.form.GWAL017 = "";
                        pageVue.form.GWAL018 = "";
                        vueData.selectOptions.gwal016Options = [];
                        app.ajax({
                            xurl: 'crm/gwai300/getGwadList',
                            xdata: {gwad001: '7', gwad003: vueData.form.GWAL015, type: '6'},
                            cb: function (result) {
                                vueData.selectOptions.gwal016Options.pushArray(result.data.data);
                            }
                        });
                    },
                    changeGwal016: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.gwal016Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.NAME016 = obj.GWADL004;
                        pageVue.form.GWAL017 = "";
                        pageVue.form.GWAL018 = "";
                        vueData.selectOptions.gwal016Options = [];
                        app.ajax({
                            xurl: 'crm/gwai300/getGwadList',
                            xdata: {gwad001: '8', gwad003: vueData.form.GWAL016, type: '7'},
                            cb: function (result) {
                                vueData.selectOptions.gwal017Options.pushArray(result.data.data);
                            }
                        });
                    },
                    changeGwal017: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.gwal017Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.NAME017 = obj.GWADL004;
                        pageVue.form.GWAL018 = "";
                    },
                    changeGwal019: function () {
                        if (!(/^\d+$/.test(vueData.form.GWAL019))) {
                            app.showWarning('邮编只允许数字!');
                            vueData.form.GWAL019 = '';
                        }
                    },
                    changeGwal020: function () {
                        if (!(/^\d+$/.test(vueData.form.GWAL020))) {
                            app.showWarning('电话只允许数字!');
                            vueData.form.GWAL020 = '';
                        }
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('gwai300_detail_edit');
                }
            });
        },

        _lazy: function () {
            this._super();

            app.ajax({
                xurl: 'crm/gwai300/getGwadList',
                xdata: {gwad001: '4', type: '3'},
                cb: function (result) {
                    vueData.selectOptions.gwal013Options.pushArray(result.data.data);
                }
            });
        }
    });

    return new Gwai300DetailEdit();

});
